﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Happy.Bases;
using Happy.Domain;
using Happy.Command;

namespace Happy.Application
{
    /// <summary>
    /// 聚合命令处理器基类。
    /// </summary>
    public class AggregateRootCommandHandler<TAggregateRootService, TAggregateRoot, TId> : ApplicationService,
        ICommandHandler<CreateCommand<TAggregateRoot, TId>>,
        ICommandHandler<UpdateCommand<TAggregateRoot, TId>>,
        ICommandHandler<DeleteCommand<TAggregateRoot, TId>>
        where TAggregateRootService : IAggregateRootService<TAggregateRoot, TId>
        where TAggregateRoot : IAggregateRoot<TId>, IHasIdProperty<TId>
    {
        /// <summary>
        /// 处理创建命令。
        /// </summary>
        public void Handle(CreateCommand<TAggregateRoot, TId> command)
        {
            this.Service<TAggregateRootService>().Create(command.Data);
        }

        /// <summary>
        /// 处理修改命令。
        /// </summary>
        public void Handle(UpdateCommand<TAggregateRoot, TId> command)
        {
            this.Service<TAggregateRootService>().Update(command.Data);
        }

        /// <summary>
        /// 处理删除命令。
        /// </summary>
        public void Handle(DeleteCommand<TAggregateRoot, TId> command)
        {
            this.Service<TAggregateRootService>().Delete(command.Data);
        }
    }
}
